<template>
  <el-card>
    <!-- 表单 -->
    <el-table
      stripe
      style="width: 100%"
      border
      :data="skuList"
      v-loading="loading"
    >
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
        row-key="id"
      >
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width" align="center">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width" align="center">
      </el-table-column>

      <el-table-column label="默认图片" width="150" align="center">
        <template slot-scope="{ row }">
          <div class="info">
            <img :src="row.skuDefaultImg" alt="商品图片" style="width: 80px" />
          </div>
        </template>
      </el-table-column>

      <el-table-column
        prop="weight"
        label="重量(KG)"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80" align="center">
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="info"
            title="上架"
            size="mini"
            icon="el-icon-top"
            v-if="row.isSale == 0"
            @click="onSale(row.id)"
          ></el-button>
          <el-button
            type="success"
            title="下架"
            v-else
            size="mini"
            icon="el-icon-bottom"
            @click="cancelSale(row.id)"
          ></el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            title="修改"
            @click="UpdateSku"
          ></el-button>
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            title="查看详情"
            @click="showSkuInfo(row.id)"
          ></el-button>
          <!-- 删除 -->
          <el-popconfirm
            :title="`确定删除 ${row.skuName} 吗`"
            @onConfirm="deleteSku(row.id)"
          >
            <el-button
              style="margin-left: 10px"
              slot="reference"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              title="删除"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 
        @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="SkuList"
      :current-page="page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="limit"
      prev-text="上一页"
      next-text="下一页"
      align="center"
      layout="prev, pager, next,jumper,->,total, sizes"
      :total="total"
    >
    </el-pagination>
    <!-- gutter:设置列的间距 -->
     <el-drawer :visible.sync="isShowSkuInfo" :with-header="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="5" >描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }} 元</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            style="margin-right: 5px"
            v-for="value in skuInfo.skuAttrValueList"
            :key="value.id"
          >
            {{ value.attrId + "-" + value.valueId }}
          </el-tag>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel class="sku-carousel" trigger="click" height="400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" alt="" style="width:100%"/>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script>
export default {
  name: "Sku",

  data() {
    return {
      page: 1, //分页器的当前页码
      limit: 5, //当前一页展示几条数据
      total: 0, //一共的数据
      skuList: [], //sku的数据
      loading: false, //加载效果

      skuInfo:{},
      isShowSkuInfo: false,//弹框的显示与隐藏
    };
  },
  mounted() {
    this.SkuList();
  },
  methods: {
    //   获取列表
    async SkuList(value = 1) {
      this.page = value;
      // 加载效果
      this.loading = true;
      let { page, limit } = this;
      let result = await this.$ajax.sku.getList(page, limit);
      this.total = result.data.total;
      this.skuList = result.data.records;
      this.loading = false;
    },
    // 修改一页多少条数据
    handleSizeChange(value) {
      this.limit = value;
      this.SkuList();
    },
    // 上架成功
    onSale(skuId) {
      this.$ajax.sku.onSale(skuId).then((result) => {
        this.$message({
          type: "success",
          message: "上架成功!",
        });
        this.SkuList(this.page);
      });
    },
    // 下架成功
    cancelSale(skuId) {
      this.$ajax.sku.cancelSale(skuId).then((result) => {
        this.$message({
          type: "success",
          message: "下架成功!",
        });
        this.SkuList(this.page);
      });
    },
    // 修改
    UpdateSku() {
      this.$message({
        type: "info",
        message: "正在开发中...",
      });
    },
    // 删除
    async deleteSku(skuId) {
      try {
        await this.$ajax.sku.remove(skuId);
        this.$message({
          type: "success",
          message: "删除SKU成功",
        });
        this.SkuList(1);
      } catch (error) {
        this.$message({
          type: "error",
          message: "删除SKU失败",
        });
      }
    },
    // 展示sku的详细信息
   async showSkuInfo(id) {
      this.isShowSkuInfo = true;
      const result = await this.$ajax.sku.get(id);
      console.log(result);
      this.skuInfo = result.data;
    },
  },
};
</script>

// <style lang="scss">
// .sku-list {
//   .el-carousel__indicator {
//     button {
//       display: inline-block;
//       width: 30px;
//       height: 30px;
//       border-radius: 50%;
//       background-color: hotpink;
//     }
//     &.is-active {
//       button {
//         background-color: purple;
//       }
//     }
//   }
// }
</style>

<style lang="scss" scoped>
// 一部分组件内的元素，我们使用类添加样式是生效的
// 还有一部分组件内的元素，我们添加样式不生效
// 原因？ 如何解决？
// 第一种解决方案： 添加样式不生效的样式全部放到style标签当中，不加scoped,
// 在跟标签添加一个类,目的是让这个样式不影响其它的组件

// 第二种解决方案（添加scoped）： 使用深度作用选择器
// 深度作用选择器的写法    ********************
//   如果是原生css 深度作用选择器  
//       父元素 >>> 选中的元素 
//   如果是less  scss 预编译的css文件
//       /deep/ 用于less
//       ::v-deep  都行


.sku-list {
  .el-row {
    height: 40px;
    .el-col {
      line-height: 40px;
      &.el-col-5 {
        font-size: 18px;
        font-weight: bold;
        text-align: right;
        margin-right: 20px;
      }
    }
  }

  .sku-carousel {
    width: 400px;
    border: 1px solid #ccc;
    img {
      width: 400px;
      height: 400px;
    }
  }

  ::v-deep .el-carousel__indicator{
    button{
      display: inline-block;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: hotpink;
    }
    &.is-active{
      button{
        background-color: purple;
      }
    }
  }
}
</style>